package com.webinsky.persist.dao.user;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.domain.user.AccountFilter;
import com.webinsky.persist.entity.user.User;

public interface UserMapper {
    @Insert({
        "insert into user (login_name, login_password, name, role, del, create_at, update_at, site_id)",
        "values (#{login_name,jdbcType=VARCHAR}, #{login_password,jdbcType=VARCHAR}, ",
        "#{name,jdbcType=VARCHAR}, #{role, jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, now(), now(), #{siteId, jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(User record);
    
    @Select({
        "select",
        "id, login_name, login_password, name, role, del, create_at, update_at, siteId",
        "from user"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
        
    })
    List<User> selectAll();
    
    @Select({
        "select",
        "id, login_name, login_password, name, role, del, create_at, update_at, site_id",
        "from user where id = #{id, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
    })
    User selectByIdAndDel(@Param("id")int id, @Param("del") int del);
    
    @Select({
        "select",
        "id, login_name, login_password, name, role, del, create_at, update_at, site_id",
        "from user where id = #{id, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
    })
    User selectById(@Param("id")int id);
    
    @Select({
        "select",
        "id, login_name, login_password, name, role, del, create_at, update_at, site_id",
        "from user where login_name = #{loginName, jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
    })
    List<User> selectByLoginName(@Param("loginName")String loginName);
    
    @SelectProvider(type = UserSQLProvider.class, method = "selectByFilter")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER)
    })
    List<User> selectByFilter(@Param("filter")AccountFilter filter);
    
    @Select({
        "select",
        "id, login_name, login_password, name, role, del, create_at, update_at, site_id",
        "from user where login_name = #{loginName, jdbcType=VARCHAR} and login_password = #{loginPassword, jdbcType=VARCHAR} and del = #{del, jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
        @Result(column="login_name", property="login_name", jdbcType=JdbcType.VARCHAR),
        @Result(column="login_password", property="login_password", jdbcType=JdbcType.VARCHAR),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="role", property="role", jdbcType=JdbcType.INTEGER),
        @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
        @Result(column="create_at", property="create_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_at", property="update_at", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
    })    
    List<User> selectByDelLoginNameAndLoginPassword(@Param("loginName") String loginName, @Param("loginPassword") String loginPassword, @Param("del") int del);
    
    @Update({
        "update user",
        "set login_name = #{login_name,jdbcType=VARCHAR},",
        "login_password = #{login_password,jdbcType=VARCHAR},",
        "name = #{name,jdbcType=VARCHAR},",
        "role = #{role,jdbcType=INTEGER},",
        "site_id = #{siteId, jdbcType=INTEGER},",
        "del = #{del,jdbcType=INTEGER},",
        "create_at = #{create_at,jdbcType=TIMESTAMP},",
        "update_at = #{update_at,jdbcType=TIMESTAMP}",
        "where Id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(User record);
    
}